<!--
 * @Author: your name
 * @Date: 2021-07-27 13:52:24
 * @LastEditTime: 2021-07-27 13:59:31
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>字典</title>
</head>

<body>
  <script>
    function Dictionary() {
      // 字典属性
      this.items = {}

      // 添加键值对
      Dictionary.prototype.set = function (key, value) {
        this.items[key] = value
      }

      // 判断字典是否有某个key
      Dictionary.prototype.has = function (key) {
        return this.items.hasOwnProperty(key)
      }

      // 从字典中移除元素
      Dictionary.prototype.remove = function (key) {
        // 1. 判断字典中是否有这个key
        if (!this.has(key)) return false

        // 2. 从字典中删除key
        Reflect.deleteProperty(this.items, key)
        return true
      }

      // 根据key去获取value
      Dictionary.prototype.get = function (key) {
        return this.has(key) ? this.items[key] : undefined
      }

      // 获取所有的keys
      Dictionary.prototype.keys = function () {
        return Object.keys(this.items)
      }

      // size方法
      Dictionary.prototype.size = function () {
        return this.keys().length
      }

      // clear方法
      Dictionary.prototype.clear = function () {
        this.items = {}
      }
    }
  </script>
</body>

</html>